﻿using Sc.Cache;
using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class CheckTypeService
    {
        private readonly CheckTypeDAO dal = new CheckTypeDAO();
		
		#region  BasicMethod
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(CheckType model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(CheckType model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			return dal.Delete(Id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }
         /// <summary>
        /// 根据条件获取前几条数据
        /// </summary>
        /// <param name="strWhere">The Query String.</param>
        /// <returns>CheckType List</returns>
        public IList<CheckType> GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }
        /// <summary>
        /// 获取派车记录
        /// </summary>
        /// <param name="orgz">机构ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="start">用车开始时间</param>
        /// <param name="end">用车结束时间</param>
        /// <param name="carNo">车牌号</param>
        /// <returns></returns>
        public CommModel<CheckType> GetList(int orgz, int pageSize, int pageIndex, string start, string end, string carNo)
        {
            string where = string.Empty;
            if (orgz > 0)
            {
                where = "AND OrgzNo=" + orgz;
            }
            if (!string.IsNullOrEmpty(start))
            {
                where += " AND CreateTime>='" + start + "' AND CreateTime<='" + end + "'";
            }
            if (!string.IsNullOrEmpty(carNo))
            {
                where += " AND CarNo Like '%" + carNo + "'";
            }
            Hashtable param = new Hashtable();
            param.Add("A1", pageIndex);
            param.Add("A2", pageSize);
            param.Add("A3", where);
            param.Add("A4", "Vw_CheckType");
            param.Add("A5", "CreateTime DESC");
            var ht = CommDAO.CommPagination(param);
            DataTable dt = ht["data"] as DataTable;
            List<CheckType> list = new List<CheckType>();
            foreach (DataRow item in dt.Rows)
            {
                list.Add(DataRowToModel(item));
            }
            CommModel<CheckType> ent = new CommModel<CheckType>();
            ent.Data = list;
            ent.PageCount = Convert.ToInt32(ht["PageCount"]);
            ent.RecordCount = Convert.ToInt32(ht["record"]);
            return ent;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CheckType DataRowToModel(DataRow row)
        {
            CheckType model = new CheckType();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Appearance"] != null)
                {
                    model.Appearance = row["Appearance"].ToString();
                }
                if (row["Gps"] != null)
                {
                    model.Gps = row["Gps"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.OrgzName = row["Name"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["HiddenRectification"] != null && row["HiddenRectification"].ToString() != "")
                {
                    model.HiddenRectification = row["HiddenRectification"].ToString();
                }
                if (row["BaseItemTypeId"] != null&& row["BaseItemTypeId"].ToString() != "")
                {
                    var id = Convert.ToInt32(row["BaseItemTypeId"]);
                    model.BaseItemName = ProjectCache.BaseItem[id].Name;
                }
                if (row["CarInfoId"] != null && row["CarInfoId"].ToString() != "")
                {
                    model.CarNo = row["CarNo"].ToString();
                    model.CarInfoId = Convert.ToInt32(row["CarInfoId"]);
                }
            }
            return model;
        }

         /// <summary>
        /// 获取单个模型
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public CheckType GetModel(int id)
        {
            var ent = this.dal.GetModel(id);
            ent.CarNo = ProjectCache.CarInfo[id].CarNo;
            ent.BaseItemName = ProjectCache.BaseItem[ent.BaseItemTypeId].Name;
            return ent;
        }
		#endregion  BasicMethod
    }
}
